package com.bgds.sys.data.iXCom29d.gateway.vo;

import com.bgds.sys.data.iXCom29d.protocol.utils.IXComActionParamDTO;
import com.cisdi.data.common.exception.BusinessException;
import com.cisdi.data.common.utils.Strings;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
public class CustomParamBuilder {
    private String encode;
    
    private List<IXComActionParamDTO> dtos = new ArrayList<IXComActionParamDTO>();

	public void setEncode(String encode) {
		this.encode = encode;
	}

	public List<IXComActionParamDTO> getDtos() {
		return dtos;
	}
	
	public void addMsgKey(String msgKey, Boolean isPassthrough, String lengthList, String propertyList, String typeList, String precisionList) {
		IXComActionParamDTO dto = new IXComActionParamDTO();
		dto.setIsPassthrough(isPassthrough);
		
		String[] lengthArray = lengthList.split(",");
		String[] propertyArray = propertyList.split(",");
		String[] typeArray = typeList.split(",");
		String[] precisionArray = precisionList.split(",");
		
		if(lengthArray.length != propertyArray.length || propertyArray.length != typeArray.length || typeArray.length != precisionArray.length) {
			throw new BusinessException("长度不一致");
		}
		
		for (int i = 0; i < lengthArray.length; i++) {
			
			String lengthStr = Strings.trim(lengthArray[i]);
			String propertyStr = Strings.trim(propertyArray[i]);
			String typeStr = Strings.trim(typeArray[i]);
			String precisionStr = Strings.trim(precisionArray[i]);
			
			IXComActionParamDTO.PropertyConfig config1 = new IXComActionParamDTO.PropertyConfig();
			config1.setFixedLength(Integer.valueOf(lengthStr));
			config1.setPropertyId(propertyStr);
			config1.setPropertyType(IXComActionParamDTO.PropertyType.valueOf(typeStr));
			config1.setPrecision(Integer.valueOf(precisionStr));
			
			dto.appendProperty(config1);
		}
		
		dtos.add(dto);
	}

	public String build() {
		IXCom29dCustomParam customParam = new IXCom29dCustomParam();
	    customParam.setEncode(encode);
		String result = customParam.toString();
		return result;
	}
    
}
